/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.sms.config;

import org.dromara.sms4j.api.SmsBlend;
import org.dromara.sms4j.api.universal.SupplierConfig;
import org.dromara.sms4j.provider.config.SmsConfig;
import org.dromara.sms4j.provider.factory.BaseProviderFactory;
import org.dromara.sms4j.solon.config.SmsBlendsInitializer;
import org.noear.solon.annotation.*;
import org.noear.solon.core.AppContext;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * <b>(MailConfiguration)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/18
 */
@Component
@Configuration
@Import(scanPackages = {"org.dromara.sms4j", "info.peigen.hotpot.component.sms", "info.peigen.hotpot.common",
        "com.gitee.peigenlpy.actable", "org.apache.ibatis"})
public class SmsConfiguration {
    @Inject
    AppContext context;

    @Bean
    protected Map<String, Map<String, Object>> blends() {
        return injectObj("hotpot.component.sms.channel", new LinkedHashMap<>());
    }

    @Bean
    protected SmsBlendsInitializer smsBlendsInitializer(List<BaseProviderFactory<? extends SmsBlend, ? extends SupplierConfig>> factoryList,
                                                        SmsConfig smsConfig,
                                                        Map<String, Map<String, Object>> blends) {
        return new SmsBlendsInitializer(factoryList, smsConfig, blends, context);
    }

    private <T> T injectObj(String prefix, T obj) {
        //@Inject 只支持在字段、参数、类型上注入
        context.cfg().getProp(prefix).bindTo(obj);
        return obj;
    }
}